﻿// -----------------------------------------------------------------------
// <copyright file="Units.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace PMSGSC.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class Unit_DAL: BaseDAL
    {

        // Lấy tất cả StockUnit
        public static List<StockUnit> GetAll(int shipID)
        {
            DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DB.StockUnits);
            return DB.StockUnits.Where(n=>n.Ship_ID == shipID).ToList();
        }
        // Lấy StockUnit theo Id
        public static StockUnit GetDataById(int id)
        {
            return DB.StockUnits.Where(m => m.StockUnit_ID == id).FirstOrDefault();
        }
        //Thêm một StockUnit
        public static bool Insert(StockUnit StockUnit)
        {
            try
            {
                DB.StockUnits.InsertOnSubmit(StockUnit);
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
        // Xóa một StockUnit
        public static bool Delete(StockUnit StockUnit)
        {
            try
            {
                DB.StockUnits.DeleteOnSubmit(StockUnit);
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
        // Cập nhật StockUnit
        public static bool Update(StockUnit StockUnit)
        {
            try
            {
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
        public static bool IsExits(StockUnit stockUnit)
        {
            return DB.StockUnits.Where(n => n.StockUnitName == stockUnit.StockUnitName && n.StockUnit_ID != stockUnit.StockUnit_ID).FirstOrDefault() != null ? true : false;
        }
        public static bool IsRelationship(StockUnit stockUnit)
        {
            PartsInEquipment part = DB.PartsInEquipments.Where(n => n.StockUnit_ID == stockUnit.StockUnit_ID).FirstOrDefault();
            if (null != part)
                return false;

            return true;
        }

    }
}
